package com.ruoyi.web.controller.api;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.exa.domain.ExaBitchUser;
import com.ruoyi.exa.service.IExaBitchUserService;
import com.ruoyi.system.domain.BaseArticle;
import com.ruoyi.system.domain.BaseBanner;
import com.ruoyi.system.domain.BaseColumn;
import com.ruoyi.system.domain.BaseTabbar;
import com.ruoyi.system.service.IBaseArticleService;
import com.ruoyi.system.service.IBaseBannerService;
import com.ruoyi.system.service.IBaseColumnService;
import com.ruoyi.system.service.IBaseTabbarService;
import com.ruoyi.web.controller.api.vo.WeatherInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("对外系统相关接口")
@RestController
@RequestMapping("/api/base")
public class ApiBaseController extends BaseController {

    @Autowired
    private IBaseTabbarService baseTabbarService;
    @Autowired
    private IBaseColumnService baseColumnService;
    @Autowired
    private IBaseArticleService baseArticleService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IBaseBannerService baseBannerService;
    /**
     * 首页底部菜单栏列表
     */
    @ApiOperation("首页底部菜单栏列表")
    @GetMapping("/baseTabbarList")
    public List<BaseTabbar> baseTabbarList()
    {
        List<BaseTabbar> list = baseTabbarService.selectBaseTabbarList(new BaseTabbar());
        return list;
    }

    /**
     * 根据底部菜单ID查询栏目列表
     */
    @ApiOperation("根据底部菜单ID查询栏目列表")
    @GetMapping("/getColumnListByTabberId")
    public TableDataInfo getColumnListByTabberId(BaseColumn baseColumn)
    {
        startPage();
        List<BaseColumn> list = baseColumnService.getColumnListByTabberId(baseColumn.getTabbarId());
        return getDataTable(list);
    }



    /**
     * 根据栏目ID查询文章列表
     */
    @ApiOperation("根据栏目ID查询文章列表")
    @GetMapping("/getArticleListByColumId")
    public TableDataInfo getArticleListByColumId(Long columId)
    {
        startPage();
        List<BaseArticle> list = baseArticleService.getArticleListByColumId(columId);
        return getDataTable(list);
    }

    /**
     * 根据文章ID查询文章详情
     */
    @ApiOperation("根据文章ID查询文章详情")
    @GetMapping("/getBaseArticleById")
    public BaseArticle getBaseArticleById(Long id)
    {
        BaseArticle baseArticle = baseArticleService.selectBaseArticleById(id);
        return baseArticle;
    }

    @ApiOperation("获取天气日报")
    @GetMapping("/weatherInfo")
    public R<WeatherInfo> weatherInfo()
    {
        Gson gson = new Gson();
        // 将JSON字符串转换为User对象
        String w = redisCache.getCacheObject("weatherInfo");
        WeatherInfo weatherInfo = gson.fromJson(w, WeatherInfo.class);
        return R.ok(weatherInfo);
    }


    /**
     * 根据type查询轮播图信息
     */
    @ApiOperation("根据type查询轮播图信息-1左侧轮播，2右侧轮播")
    @GetMapping("/getBannerByType")
    public List<BaseBanner> getBannerByType(String type)
    {
        BaseBanner baseBanner = new BaseBanner();
        baseBanner.setType(type);
        return baseBannerService.selectBaseBannerList(baseBanner);
    }
}
